/*
 * @Author: fhw
 * @Date: 2022-07-26 10:13:58
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-02-20 11:46:02
 * @Description:
 */
'use strict';

import { HttpException, HttpStatus } from '@nestjs/common';
/** 是否中文 */
export function isChinese(temp) {
  if (/.*[\u4e00-\u9fa5]+.*$/.test(temp)) {
    return true;
  }
  return false;
}
/** 报错 */
export function throwHttp(message: string) {
  throw new HttpException(
    {
      status: HttpStatus.BAD_REQUEST,
      message: message,
    },
    HttpStatus.BAD_REQUEST,
  );
}
export interface StatusCode {
  status: 400 | 200;
  message: string;
  success: boolean;
}
/** 错误状态 */
export function errorState(message: string): StatusCode {
  return {
    status: 400,
    message,
    success: false,
  };
}
/** 成功状态 */
export function successState(message: string): StatusCode {
  return {
    status: 200,
    message,
    success: true,
  };
}
interface Pagination {
  pageSize: number;
  pageNum: number;
}
/** 分页处理 */
export function getPagination({
  pageSize = 10,
  pageNum = 1,
}: Pagination): Pagination {
  const size = Number(pageSize) ?? 10;
  let num = Number(pageNum) ?? 1
  if (num !== 0) {
    num -= 1;
  }
  return {
    pageSize: size,
    pageNum: num,
  };
}
/**
 * 校验密码
 * @param {string} value 密码
 * @param {Function} callback 回调
 * @returns
 */
export function checkPassword(value: string) {
  let level = '';
  // 校验是数字
  const regex1 = /^\d+$/;
  // 校验字母
  const regex2 = /^[A-Za-z]+$/;
  // 校验符号
  const regex3 =
    /^[`~!@#$%^&*()_\-+=<>?:"{}|,./;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘'，。、]+$/;
  if (regex1.test(value)) {
    level = '低';
  } else if (regex2.test(value)) {
    level = '低';
  } else if (regex3.test(value)) {
    level = '低';
  } else if (/^[A-Za-z\d]+$/.test(value)) {
    level = '中';
  } else if (
    /^[`~!@#$%^&*()_\-+=<>?:"{}|,./;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘'，。、\d]+$/.test(
      value,
    )
  ) {
    level = '中';
  } else if (
    /^[`~!@#$%^&*()_\-+=<>?:"{}|,./;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘'，。、A-Za-z]+$/.test(
      value,
    )
  ) {
    level = '中';
  } else if (
    /^[`~!@#$%^&*()_\-+=<>?:"{}|,./;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘'，。、A-Za-z\d]+$/.test(
      value,
    )
  ) {
    level = '高';
  }
  return level;
}
export function toDate(date) {
  return new Date(date)
}
function padZero(value: number): string {
  return String(value).padStart(2, '0');
}
// 时间格式化 yyyy-MM-dd
export function formatDate(dateString: Date, format = 'yyyy/MM/dd hh:mm:ss') {
  const date = new Date(dateString);

  // 检查日期是否有效
  if (isNaN(date.getTime())) {
    return 'Invalid Date';
  }

  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hours = date.getHours();
  const minutes = date.getMinutes();
  const seconds = date.getSeconds();

  // 替换格式中的占位符
  const formattedDate = format
    .replace('YYYY', year.toString())
    .replace('MM', padZero(month))
    .replace('DD', padZero(day))
    .replace('HH', padZero(hours))
    .replace('mm', padZero(minutes))
    .replace('ss', padZero(seconds));

  return formattedDate;
}

import { Request } from 'express';

// 辅助函数，用于生成链接
export function url_for(req: Request, path: string): string {
  const protocol = req.protocol;
  const host = req.get('host');
  return `${protocol}://${host}${path}`;
}
